package com.paomi.client.activity.search.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.paomi.client.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/12/21
 * Describe:
 * com.paomi.client.activity.search.adapter
 */
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> {
    Context context;
    List<String> mlist = new ArrayList<>();
    List<String> historyList = new ArrayList<>();
    OnItemClick onClickListener;
    private Typeface mType;
    public void OnItemClick(OnItemClick onClickListener) {
        this.onClickListener = onClickListener;
    }


    public SearchListAdapter(Context context, Typeface typeface) {
        this.context = context;
        this.mType = typeface;
    }

    public void setData(List<String> list,List<String> history) {
        this.mlist = list;
        this.historyList = history;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.search_history_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String ticket = mlist.get(position);
        holder.history_list.setLayoutManager(new GridLayoutManager(context,5));
        SearchGridAdapter gridAdapter=new SearchGridAdapter(context,mType);
        holder.history_list.setAdapter(gridAdapter);
        gridAdapter.setData(historyList);
        gridAdapter.OnItemClick(new SearchGridAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                if(onClickListener!=null){
                    onClickListener.setOnItemClick(view,pos);
                }
            }
        });
        holder.search_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null){
                    onClickListener.setOnItemDelete(holder.search_delete,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.search_title)
        TextView search_title;
        @BindView(R.id.search_delete)
        ImageView search_delete;
        @BindView(R.id.history_list)
        RecyclerView history_list;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int pos);
        void setOnItemDelete(View view, int pos);
    }
}
